﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Training.Dia1.Api.Models;
using Training.Dia1.Data.Managers;

using System.Threading.Tasks;
using System.Web.Http.Description;
using Training.Dia1.Data.Entities;

namespace Training.Dia1.Api.Controllers
{
    public class ProductController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<ProductModel> Get()
        {
            return ProductManager.GetList("").Select(t => new ProductModel
            {
                Id = t.Id,
                Name = t.Name,
                Detail = t.Detail,
                Price = t.Price,

                MeasureUnitId = t.MeasureUnitId,
                MeasureUnitModel = new MeasureUnitModel
                {
                    Id = t.MeasureUnit.Id,
                    Name = t.MeasureUnit.Name,
                    Abbreviation = t.MeasureUnit.Abbreviation,
                },
                
            });
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public ProductModel Post([FromBody]ProductModel m)
        {
            Product t = ProductManager.Create(new Product
            {
                Name = m.Name,
                Price = m.Price,
                Detail = m.Detail,
                MeasureUnitId = m.MeasureUnitId,
            });
            return new ProductModel
            {
                Id = t.Id,
                Name = t.Name,
                Detail = t.Detail,
                Price = t.Price,
                MeasureUnitId = t.MeasureUnitId,
                MeasureUnitModel = new MeasureUnitModel
                {
                    Id = t.MeasureUnit.Id,
                    Name = t.MeasureUnit.Name,
                    Abbreviation = t.MeasureUnit.Abbreviation,
                },
            };
        }

        // PUT api/<controller>/5
        public ProductModel Put(Guid id, [FromBody]ProductModel m)
        {
            Product t = ProductManager.Update(new Product
            {
                Id = id,
                Name = m.Name,
                Price = m.Price,
                Detail = m.Detail,
                MeasureUnitId = m.MeasureUnitId,
            });

            return new ProductModel
            {
                Id = t.Id,
                Name = t.Name,
                Detail = t.Detail,
                Price = t.Price,
                MeasureUnitId = t.MeasureUnitId,
                MeasureUnitModel = new MeasureUnitModel
                {
                    Id = t.MeasureUnit.Id,
                    Name = t.MeasureUnit.Name,
                    Abbreviation = t.MeasureUnit.Abbreviation,
                },
            };
             
        }

        // DELETE api/<controller>/5
        public void Delete(Guid id)
        {
            ProductManager.Delete(id);
        }
    }
}